


/***

    把用户提交的代码直接运行，

    // 可以通过系统调用的方式，吧自己的脚本放到服务器上，
    exec*("")

    if(exex fork)

    docker 

docker build -t online-judge-runner-image ./
docker run --rm -v ./app:/app cpp-runner bash -c 'g++ -std=c++17 -O2 -Wall -o main main.cpp >error.txt 2>&1' 
docker run --rm --memory=128m --cpu-quota=50000 --network=none -v ./app:/app cpp-runner bash -c 'timeout 2s ./main < input.txt > output.txt 2> error.txt'


bash -c "sh -c ./main < input > output 2 > error";
pid = 1 : bash -> sh -c -> main
pid = 1 :init

*/



// #include "compile-run.h"
#include <string>
#include <log.h>
#include "brpc.h"
#include "compile.pb.h"
#include "etcd_path.h"


int main() {
    // bool ret = lyt::Runner().systemcall("docker run --rm -v ./app:/app cpp-runner bash -c 'g++ -std=c++17 -O2 -Wall -o 1234 1234.cc >error.txt 2>&1'");
    // if (ret == false) {
    //     infolog << "执行失败";
    // }
    // else {
    //     infolog << "执行成功";
    // }
    // std::string ret;
    // std::ifstream ifs("./app/1234.cc");
    // std::getline(ifs,ret);
    // infolog << ret;
    // std::getline(ifs,ret);
    // infolog << ret;
    lyt::EtcdNamingService name_server;
    brpc::NamingServiceExtension()->RegisterOrDie("etcd", &name_server);
    infolog << "1";
    compile::CompileRequest req;
    req.set_code(R"(
#include <iostream>
using namespace std;

int main() {    
    cout << 3 / 0 << endl;
    return 0;
}
)"
    );
    req.set_input("4");
    compile::CompileResponse res;
    std::shared_ptr<brpc::Channel> _channel;
    std::shared_ptr<compile::CompileService> _compileservice_stub;
    infolog << "2";
    _channel = lyt::build_etcd("127.0.0.1:2379", lyt::compile_path);
    infolog << "3"; 
    _compileservice_stub = make_shared<compile::CompileService_Stub>(_channel.get());
    brpc::Controller cntl;
    _compileservice_stub->Compile(&cntl, &req, &res, nullptr);
    infolog << "compile success";
    if(cntl.Failed()){
        errorlog << "服务调用失败!" ;
        return -1;
    }
    infolog << "stats: " << res.status();
    infolog << "error message: " << res.err_msg();
    infolog << "result: " << res.result();
    return 0;
}